package com.atguigu.juc.day02.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Demo08LockInterruptibly {

    public static void main(String[] args) throws InterruptedException {

        // 1、创建锁对象
        Lock lock = new ReentrantLock();

        // 2、创建 A 线程占用锁
        new Thread(()->{
            lock.lock();
        }, "thread-a").start();

        // 3、创建 B 线程以支持响应中断的方式申请锁
        Thread thread = new Thread(() -> {

            try {
                lock.lockInterruptibly();

            } catch (InterruptedException e) {
                System.out.println(Thread.currentThread().getName() + " 阻塞状态被打断了。");
            }

        }, "thread-b");

        thread.start();

        // 4、主线程尝试打断阻塞状态
        TimeUnit.SECONDS.sleep(5);

        System.out.println("5 秒时间到。");

        thread.interrupt();
    }

}
